package com.system.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.controller.ReservePlandrawingController;
import com.system.mapper.ReservePlandrawingMapper;
import com.system.po.ReservePlan;
import com.system.po.info.DocumentFile;
import com.system.service.ReservePlandrawingService;
import com.system.utils.DWGFileToPDFManager;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class ReservePlandrawingServiceImpl implements ReservePlandrawingService {
    @Autowired
    private ReservePlandrawingMapper reservePlandrawingMapper;
    @Override
    public boolean addReservePlandrawing(ReservePlan reservePlan) {
        return reservePlandrawingMapper.addReservePlandrawing(reservePlan);

    }

    @Override
    public boolean updateReservePlandrawingById(ReservePlan reservePlan) {
        System.out.println("开始修改");
        return reservePlandrawingMapper.updateReservePlandrawingById(reservePlan);
    }

    @Override
    public PageInfo<ReservePlan> selectReservePlandrawingList(Integer page,Integer limit) {

        PageHelper.startPage(page<=0?page=1:page,limit<=0?limit=1:limit);
        List<ReservePlan> byPage = reservePlandrawingMapper.selectReservePlandrawingList();
        PageInfo<ReservePlan> page2=new PageInfo<>(byPage);
        return page2;
    }

    @Override
    public ReservePlan selectReservePlandrawingById(Integer id) {
        return reservePlandrawingMapper.selectReservePlandrawingById(id);
    }

    @Override
    @Async
    public void uploadAndDWGFileToPDFManager(MultipartFile myfile, String originalFilename) throws IOException {
        File file=new File(originalFilename);
        myfile.transferTo(file);

//        CommonsMultipartFile cf= (CommonsMultipartFile)myfile; //MultipartFile转File
//        DiskFileItem fi = (DiskFileItem)cf.getFileItem();
//        File f = fi.getStoreLocation();
        System.out.println("写文件");
        DWGFileToPDFManager.DWGFileToPDF(file);
        System.out.println("写文件后");
        //DWGFileToPDFManager.DWGFileToPDF2(file);
    }


    @Override
    public Integer DeleteReservePlandrawing(Integer id) {
        Integer integer = reservePlandrawingMapper.DeleteReservePlandrawing(id);
        return integer;
    }

}
